/**
 * @file {pantie目标库flaw表model}
 */
 'use strict'

 const { DataTypes, Model, Op } = require('../../../azeroth').sequelize;
 const {flawInitData} = require('../common/init_data');
 class FlawPiantie extends Model { }
 
 /**
  * @desc 映射需要的字段 【源库的 部分字段 --> 目标库字段】
  */
  const initFlawData = (sequelize_aim) => {
    FlawPiantie.init(
        ...flawInitData,
        {
            freezeTableName: true,
            sequelize: sequelize_aim,
            modelName: 'flaw',
            timestamps: false
        }
     )
  }
  FlawPiantie.getLatestFlawId = async (sequelize_aim) => {
    let sql = 'select max(uid) as uid from flaw';
    let res = await sequelize_aim.query(sql, { type: sequelize_aim.QueryTypes.SELECT });
    return res[0].uid;
}
  /**
   * get flaw information by panel_id
   * @param {sequlize} sequelize_source 
   * @param {String} panelId 
   * @returns 
   */
  FlawPiantie.getFlawData = async (sequelize_source, queryStr) => {
    let query = typeof(queryStr) == 'string' ? `b.panel_id = "${queryStr}"` : `a.uid > ${queryStr} and a.uid <= (select max(uid) from flaw)`;

    let sql = 'select a.uid, a.jobId as job_id, b.panel_id, a.dCD as center_pos_cd, a.dMD as center_pos_md, a.dLength as length_md,' +
    ' a.dWidth as length_cd, a.flawClassType as flaw_class_type, a.createTime as gen_time, a.cameraId as camera_id, b.lot_id, a.dArea as area, a.content as save_path, a.dDiameter as diameter ' +
    'from flaw a inner join panel_info b on a.doffId = b.doff_id where ' + query;
    let res = await sequelize_source.query(sql, { type: sequelize_source.QueryTypes.SELECT });
    res.forEach(item => {
      item.gen_time_str = item.gen_time.toString();
    })
    return res;
  }
  /**
   * 上传flaw信息
   * @param {Array} data 上传的缺陷信息数组
   * @returns 
   */
  FlawPiantie.uploadPolInfo = async (data) => {
    // ignoreDuplicates: 忽略重复数据
    let res = await FlawPiantie.bulkCreate(data, {ignoreDuplicates : true});
    return res;
  }
 module.exports = {
     FlawPiantie,
     initFlawData
 };